//
//  LeftTableViewCell.m
//  MemoryTravel
//
//  Created by dllo on 15/12/23.
//  Copyright © 2015年 fcx. All rights reserved.
//

#import "LeftTableViewCell.h"

@implementation LeftTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    if (self) {

        [self createSubViews];
    }

    return self;
}

- (void)createSubViews{

    self.contentView.backgroundColor = [UIColor colorWithRed:230 / 255.0 green:236 / 255.0 blue:242 / 255.0 alpha:1.0];

    self.headerImage = [UIImageView new];
    self.headerImage.layer.masksToBounds = YES;//裁掉边界
    self.headerImage.layer.cornerRadius = 5;//设置半径
    [self.contentView addSubview:self.headerImage];

    // 适应图片的像素
    self.headerImage.contentMode = UIViewContentModeScaleAspectFill;

    [self.headerImage mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.mas_equalTo(10);
        make.top.mas_equalTo(5);
        make.width.mas_equalTo(Width - 20);
        make.height.equalTo(self.contentView.mas_height).offset(-10);
    }];

    self.namelabel = [UILabel new];
    self.namelabel.numberOfLines = 0;
    self.namelabel.font = [UIFont systemFontOfSize:20];
    self.namelabel.textColor = [UIColor whiteColor];
    [self.contentView addSubview:self.namelabel];
    self.namelabel.layer.masksToBounds = YES;//裁掉边界
    self.namelabel.layer.cornerRadius = 5;//设置半径

    [self.namelabel mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.mas_equalTo(20);
        make.top.mas_equalTo(20);
        make.right.mas_equalTo(-20);
    }];

    self.otherLabel = [UILabel new];
    self.otherLabel.font = [UIFont systemFontOfSize:12];
    self.otherLabel.textColor = [UIColor whiteColor];
    self.otherLabel.numberOfLines = 0;
    self.otherLabel.layer.masksToBounds = YES;//裁掉边界
    self.otherLabel.layer.cornerRadius = 5;//设置半径
    [self.contentView addSubview:self.otherLabel];

    [self.otherLabel mas_remakeConstraints:^(MASConstraintMaker *make) {

        make.left.equalTo(self.namelabel.mas_left);
        make.top.equalTo(self.namelabel.mas_bottom).offset(10);
    }];

    UIView *sideView = [UIView new];
    sideView.backgroundColor = [UIColor whiteColor];
    sideView.layer.masksToBounds = YES;
    sideView.layer.cornerRadius = 22;
    [self.contentView addSubview:sideView];

    [sideView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.size.mas_equalTo(CGSizeMake(44, 44));
        make.left.mas_equalTo(20);
        make.bottom.mas_equalTo(-20);
    }];

    self.personImage = [UIImageView new];
    self.personImage.layer.masksToBounds = YES;
    self.personImage.layer.cornerRadius = 20;
    [sideView addSubview:self.personImage];

    [self.personImage mas_makeConstraints:^(MASConstraintMaker *make) {

        make.size.mas_equalTo(CGSizeMake(22, 22));
        make.edges.mas_equalTo(UIEdgeInsetsMake(3, 3, 3, 3));
    }];

    //渐变
    self.gradientLayer = [CAGradientLayer layer];
    self.gradientLayer.frame = CGRectMake(0, 0, Width, 100);
    [self.headerImage.layer addSublayer:self.gradientLayer];

    //设置渐变颜色方向
    self.gradientLayer.startPoint = CGPointMake(0, 1);

    self.gradientLayer.endPoint = CGPointMake(0, 0);
    //设定颜色组
    self.gradientLayer.colors = @[(__bridge id)[UIColor clearColor].CGColor,
                                  (__bridge id)[UIColor blackColor].CGColor];

    //设定颜色分割点
    self.gradientLayer.locations = @[@(0.1f) ,@(1.0f)];
}

- (void)setTravelsModel:(TravelsModel *)travelsModel{

    _travelsModel = travelsModel;

    //图片
    NSURL *urlHeader = [NSURL URLWithString:travelsModel.front_cover_photo_url];

    [self.headerImage sd_setImageWithURL:urlHeader placeholderImage:[UIImage imageNamed:@"placeHoderImage.png"]];

    self.namelabel.text = travelsModel.name;

    NSString *time;

    if (self.travelsModel.start_date == nil || [self.travelsModel.start_date isEqualToString:@"(null)"]) {
        time = [NSString stringWithFormat:@"%@天/%@图",travelsModel.days,travelsModel.photos_count];
    }else{
        time = [NSString stringWithFormat:@"%@/%@天/%@图",travelsModel.start_date, travelsModel.days, travelsModel.photos_count];
    }
    self.otherLabel.text = time;
    //图片
    NSURL *url = [NSURL URLWithString:travelsModel.anotherUser.image];

    [self.personImage sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeHoderImage.png"]];
}




@end
